<template>
  <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="40%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="企业类型" prop="type">
        <el-input v-model="form.type" placeholder="请输入企业类型"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="text">
        <el-input v-model="form.text" placeholder="请输入企业名称"></el-input>
      </el-form-item>
      <el-form-item label="企业分类" prop="enterprise_type">
        <el-select v-model="form.enterprise_type" placeholder="请选择企业分类" clearable style="width: 100%">
          <el-option label="瞪羚" value="瞪羚"></el-option>
          <el-option label="独角兽" value="独角兽"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "EnterpriseAdd",
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增企业信息",
      isEdit: false,
      form: {
        type: "",
        text: "",
        enterprise_type: ""
      },
      rules: {
        type: [
          { required: true, message: "请输入企业类型", trigger: "blur" },
          { max: 20, message: "企业类型不能超过20个字符", trigger: "blur" }
        ],
        text: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          { max: 100, message: "企业名称不能超过100个字符", trigger: "blur" }
        ],
        enterprise_type: [
          { max: 20, message: "企业分类不能超过20个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    open(row) {
      this.dialogVisible = true;
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑企业信息";
        this.form = { ...row };
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增企业信息";
        this.form = {
          type: "",
          text: "",
          enterprise_type: ""
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.enterprise.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.enterprise.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            if (error.response && error.response.data && error.response.data.message) {
              this.$message.error(error.response.data.message);
            } else {
              this.$message.error("操作失败，请检查企业名称是否重复");
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
